﻿Imports System.Windows.Forms

Public Class frmMCiudadws
    Public arrConsulta(2) As String
    Private arrCampos() As String = {"ciudad_id", "ciudad_nombre", "departamento_id"}

    Private Sub frmMCiudad_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        mdiPrincipal.tsmiCiudadws.Enabled = True
    End Sub

    Private Sub frmMCiudad_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.refreshList()
    End Sub

    Private Sub tsbNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbNuevo.Click
        Me.tsbNuevo.Enabled = False
        renderForm("Nuevo - Ciudad", 1)
    End Sub

    Private Sub tsbConsultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbConsultar.Click
        Me.tsbConsultar.Enabled = False
        renderForm("Consultar - Ciudad", 2)
    End Sub

    Private Sub tsbEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbEditar.Click
        Dim strId As String = Me.dgvCiudad.Rows(Me.dgvCiudad.CurrentRow.Index).Cells(0).Value.ToString()

        If (strId <> "") Then
            Me.tsbEditar.Enabled = False
            renderForm("Editar - Ciudad", 3)
        Else
            MsgBox("Debe seleccionar un registro", MsgBoxStyle.Information, "Ciudad")
        End If
    End Sub

    Private Sub tsbBorrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbBorrar.Click
        Dim objResult = MsgBox("Esta seguro que desea borrar el registro seleccionado?", MsgBoxStyle.YesNo, "Borrar - Ciudad")

        If (objResult = DialogResult.Yes) Then
            Dim strId As String = Me.dgvCiudad.Rows(Me.dgvCiudad.CurrentRow.Index).Cells(0).Value.ToString

            mdlConexion.blnOnDB = True
            mdlCRUD.delRecord("ciudad", "ciudad_id = '" & strId & "'")
            mdlConexion.blnOnDB = False

            If (mdlCRUD.blnFlag = True) Then
                Me.refreshList()
            End If
        End If
    End Sub

    Private Sub tsbRecargar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbRecargar.Click
        arrConsulta(0) = ""
        arrConsulta(1) = ""
        arrConsulta(2) = ""
        Me.refreshList()
    End Sub

    Private Sub renderForm(ByVal strTitle As String, ByVal intMode As Integer)
        Dim frmCiudad As New frmCiudadws

        frmCiudad.MdiParent = mdiPrincipal
        frmCiudad.frmParent = Me

        Dim dstRecord As New DataSet
        dstRecord.Tables.Add("Departamento")

        mdlConexion.blnOnDB = True
        mdlCRUD.loadRecord(dstRecord, "Departamento", "departamento", "", "departamento_nombre")
        mdlConexion.blnOnDB = False

        frmCiudad.cmbDepartamento.DataSource = dstRecord.Tables("Departamento")
        frmCiudad.cmbDepartamento.DisplayMember = dstRecord.Tables("Departamento").Columns(1).Caption.ToString
        frmCiudad.cmbDepartamento.ValueMember = dstRecord.Tables("Departamento").Columns(0).Caption.ToString

        frmCiudad.modeForm = intMode
        frmCiudad.Text = strTitle
        frmCiudad.Show()
    End Sub

    Public Sub refreshList()
        Dim dstRecord As New DataSet
        Dim strCondicion As String = ""
        dstRecord.Tables.Add("Ciudad")

        For i As Integer = 0 To 2
            If (Me.arrConsulta(i) <> "") Then
                strCondicion &= IIf(strCondicion <> "", " AND ", "") & IIf(i = 2, Me.arrCampos(i) & " = '" & LCase(Me.arrConsulta(i)) & "'", "LOWER(" & Me.arrCampos(i) & "::text) LIKE '%" & LCase(Me.arrConsulta(i)) & "%'")
            End If
        Next

        mdlConexion.blnOnDB = True
        mdlCRUD.loadRecord(dstRecord, "Ciudad", "vistaciudad", strCondicion, "ciudad_nombre")
        mdlConexion.blnOnDB = False

        Me.dgvCiudad.DataSource = dstRecord.Tables("Ciudad")

        Me.dgvCiudad.Columns(2).Visible = False
        Me.dgvCiudad.Columns(0).HeaderText = "Codigo"
        Me.dgvCiudad.Columns(1).HeaderText = "Ciudad"
        Me.dgvCiudad.Columns(3).HeaderText = "Departamento"
        Me.dgvCiudad.Columns(0).Width = 60
        Me.dgvCiudad.Columns(1).Width = 260
        Me.dgvCiudad.Columns(3).Width = 260
    End Sub

End Class